package com.Cychat.auth.controller;


import com.Cychat.auth.entity.DTO.LoginParamsDTO;
import com.Cychat.auth.entity.VO.LoginParamsVO;
import com.Cychat.auth.params.LoginParams;
import com.Cychat.auth.service.ALiYunOssSevice;
import com.Cychat.auth.service.LoginService;
import com.Cychat.auth.service.UserService;
import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Auther: 刘博文
 * Date: 2025/10/28:19:46
 * Description: 登录控制类
 */
@RequestMapping("/api/user")
@RestController
@CrossOrigin
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private ALiYunOssSevice aliYunOssSevice;

    /**
     * 微信小程序登录接口
     * @param code 微信登录凭证
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResultMessage<LoginParamsVO> wechatLogin(@RequestBody LoginParams code,
                                                    HttpServletResponse response ) {
            return loginService.login(code, response);
    }
    /**
     * 上传用户头像
     * @param file 用户头像
     * @return 上传结果
     */
    @PostMapping("/uploadAvatar/{userId}")
    public ResultMessage<String> uploadAvatar(@RequestParam("file") MultipartFile file,
                                             @PathVariable("userId") Long userId) {
        try {
            //调用service层获取oss返回访问图片链接
            String uploadAvatarUrl = aliYunOssSevice.updateAvatar(file, userId);
            if (uploadAvatarUrl != null && !uploadAvatarUrl.isEmpty()) {
                return ResultMessage.success(uploadAvatarUrl);
            } else {
                // 头像上传失败，可以选择抛出异常或记录日志
                log.warn("头像上传失败，用户: " + userId);
            }
            return ResultMessage.error("上传头像失败");
        } catch (IOException e) {
            log.error("上传头像异常：", e);
        }
        return ResultMessage.error("上传头像失败");
    }
/**
 * 更新用户信息
 * @param userDTO 用户信息
 * @return 更新结果
 */
@PutMapping("/update/{userId}")
public ResultMessage<String> updateUser(@RequestBody LoginParamsDTO userDTO,
                                        @PathVariable("userId") Long userId) {
    try {
        // 将文件设置到userDTO中
        int i = userService.updateUser(userDTO, userId);
        if (i > 0) {
            return ResultMessage.success("更新用户信息成功");
        } else {
            return ResultMessage.error("更新用户信息失败");
        }
    } catch (Exception e) {
        log.error("更新用户信息失败", e);
        return ResultMessage.error("更新用户信息失败: " + e.getMessage());
    }
}
/**
 * Description : 用户登出将token加入redis的黑名单
 * ChangeLog : 1. 创建 (2025/11/5 11:57 [Esang]);
 * @param userId 用户id
 * @return null
*/
@PostMapping("/logout/{userId}")
    public ResultMessage<String> logout( HttpServletRequest request,
                                        @PathVariable("userId") Long userId) {
        return loginService.logout(request);
    }
}
